package com.lab.network.proxy;

import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;

/**
 * Created by MiJack on 2015/11/20.
 */
public class ApiProxyCallBack<T> implements IProxyCallback {

    public ApiProxyCallBack() {
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        //预处理
        CtApiResponse response = (CtApiResponse) result.getData();
        doPreWork(response);
        try {
            if (response.isResponseNormal()) {
                T data = (T) response.result;
                return onSuccess(data, response);
            } else {
                return onFaild(response);
            }
        } catch (Throwable e) {
            //发生异常
            e.printStackTrace();
            handleThrowable(response, e);
        }

        return false;
    }

    public void doPreWork(CtApiResponse response) {
    }

    public boolean onSuccess(T data, CtApiResponse response) {
        return false;
    }

    public boolean onFaild(CtApiResponse response) {
        return false;
    }

    public void handleThrowable(CtApiResponse response, Throwable e) {
        throw  new RuntimeException(e);
    }
}
